﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MediaManager
{
    public partial class DongmaiSearchFrm : Form
    {
        DataClasses1DataContext _dataContext = new DataClasses1DataContext();
        private Manage _manage = null;
        private List<BaseTable> _listBasetable = null;
        public DongmaiSearchFrm(Manage manage)
        {
            InitializeComponent();
            _manage = manage;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            Init();
        }
        private void Init()
        {
            int count;
            foreach (Control c in groupBox1.Controls)
            {
                if (c is ComboBox)
                {
                    if (c is PresentationControls.CheckBoxComboBox)
                    {
                        count = (c as PresentationControls.CheckBoxComboBox).CheckBoxItems.Count;
                        for (int i = 1; i < count; i++)
                        {
                            (c as PresentationControls.CheckBoxComboBox).CheckBoxItems[i].Checked = false;
                        }
                    }
                    else
                    {
                        (c as ComboBox).SelectedIndex = -1;
                    }
                }
            }
            foreach (Control c in groupBox3.Controls)
            {
                if (c is ComboBox)
                {
                    if (c is PresentationControls.CheckBoxComboBox)
                    {
                        count = (c as PresentationControls.CheckBoxComboBox).CheckBoxItems.Count;
                        for (int i = 1; i < count; i++)
                        {
                            (c as PresentationControls.CheckBoxComboBox).CheckBoxItems[i].Checked = false;
                        }
                    }
                    else
                    {
                        (c as ComboBox).SelectedIndex = -1;
                    }
                }
            }
            dataGridView1.DataSource = null;
            dataGridView1.Refresh();
        }

        private void DongmaiSearchFrm_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string comboxNameIndex = string.Empty;
                List<DongMaiSystem> listdongmaiSystem = _dataContext.DongMaiSystem.Where(p => true).ToList();
                bool b = false;
                int count;
                if (listdongmaiSystem.Count > 0)
                {
                    foreach (Control c in groupBox1.Controls)
                    {
                        if (c is ComboBox)
                        {
                            if (c is PresentationControls.CheckBoxComboBox)
                            {
                                if ((c as PresentationControls.CheckBoxComboBox).Text.Length > 0)
                                {
                                    b = true;
                                    count = Global.Instance.GetCheckComboboxCount(c as PresentationControls.CheckBoxComboBox);
                                    comboxNameIndex += (c as PresentationControls.CheckBoxComboBox).Name;
                                    for (int i = 1; i < count; i++)
                                    {
                                        comboxNameIndex += " " + Convert.ToInt32((c as PresentationControls.CheckBoxComboBox).CheckBoxItems[i].Checked).ToString();
                                    }
                                    listdongmaiSystem = listdongmaiSystem.Where(p => p.comboxIndex.Contains(comboxNameIndex)).ToList();
                                    if (listdongmaiSystem.Count < 1)
                                    {
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                if ((c as ComboBox).SelectedIndex > -1)
                                {
                                    b = true;
                                    comboxNameIndex = (c as ComboBox).Name + " " + (c as ComboBox).SelectedIndex.ToString().Trim();
                                    listdongmaiSystem = listdongmaiSystem.Where(p => p.comboxIndex.Contains(comboxNameIndex)).ToList();
                                    if (listdongmaiSystem.Count < 1)
                                    {
                                        break;
                                    }
                                }
                            }
                        }
                        
                    }
                    if (listdongmaiSystem.Count > 0)
                    {
                        foreach (Control c in groupBox3.Controls)
                        {

                            if (c is ComboBox)
                            {
                                if (c is PresentationControls.CheckBoxComboBox)
                                {
                                    if ((c as PresentationControls.CheckBoxComboBox).Text.Length > 0)
                                    {
                                        b = true;
                                        count = Global.Instance.GetCheckComboboxCount(c as PresentationControls.CheckBoxComboBox);
                                        comboxNameIndex += (c as PresentationControls.CheckBoxComboBox).Name;
                                        for (int i = 1; i < count; i++)
                                        {
                                            comboxNameIndex += " " + Convert.ToInt32((c as PresentationControls.CheckBoxComboBox).CheckBoxItems[i].Checked).ToString();
                                        }
                                        listdongmaiSystem = listdongmaiSystem.Where(p => p.comboxIndex.Contains(comboxNameIndex)).ToList();
                                        if (listdongmaiSystem.Count < 1)
                                        {
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    if ((c as ComboBox).SelectedIndex > -1)
                                    {
                                        b = true;
                                        comboxNameIndex = (c as ComboBox).Name + " " + (c as ComboBox).SelectedIndex.ToString().Trim();
                                        listdongmaiSystem = listdongmaiSystem.Where(p => p.comboxIndex.Contains(comboxNameIndex)).ToList();
                                        if (listdongmaiSystem.Count < 1)
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                            
                        }
                    }

                }
                if (b && listdongmaiSystem.Count > 0)
                {
                    var result = from p in _dataContext.BaseTable.ToList()
                                 from q in listdongmaiSystem
                                 where p.编号 == q.id
                                 select p;
                    _listBasetable = result.ToList();
                    dataGridView1.DataSource = _listBasetable;
                    dataGridView1.Refresh();
                }
                else
                {
                    dataGridView1.DataSource = null;
                    dataGridView1.Refresh();
                    _listBasetable = null;
                }
            }
            catch (Exception ex)
            {
                GlobalBase.Global.Instance.ShowMessage(ex.Message);
                GlobalBase.CatchException.DealWithException(ex);
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            Global.Instance.ShowFormID(dataGridView1, _listBasetable, _manage);
        }
    }
}
